/**
 * Created by xiewl on 2016/5/10.
 */
var urlParamObj = $$.util.urlParamToObj();
//计算form-box的高度
var calcFormHeight = function () {
    var clientHeight = document.body.clientHeight;
    var formEle = $('.form-box').get(0);
    var headerHeight = formEle.offsetTop + formEle.offsetParent.offsetTop;
    $('.form-box').css('height',clientHeight - headerHeight - 5);
};
//模版作者匹配信息
var matchAuthorInfo = function () {
    var clickTime = 0;
    $('#inputAuthor').keyup(function () {
        var nowTime = new Date().getTime();
        if((nowTime - clickTime) < 100) return false;
        var str = $(this).val().trim();
        if(str){
            new followupPlan().searchDoctorList(str,function (res,status) {
               if(status == 'success'){
                   res = JSON.parse(res);
                   var list = JSON.parse(res.data);
                   var html = '';
                   for(var i =0,len=list.length;i<len;i++){
                       html += [
                           '<p class="author-item" data-id="'+ list[i].rid +'"><span class="doctor-name">'+ list[i].name +'</span>（'+ list[i].hospital +'-'+ list[i].dept +'）</p>'
                       ].join('');
                   }
                   $('.author-list').html(html).show();
               }
            });
        }else{
            $('.author-list').hide();
        }
    });
    $('.author-list').on('click','.author-item',function () {
        $('#inputAuthor').val($(this).find('.doctor-name').text()).attr({'data-id': $(this).attr('data-id'),'data-name': $(this).find('.doctor-name').text()});
        $('.author-list').hide();
    })
};
//保存模板基本信息
var setTemplateBase = function () {
    $('#submitForm').click(function () {
        var doctorid = $('#inputAuthor').attr('data-id');
        var doctorusername = $('#inputAuthor').val();
        var name = $('#inputName').val();
        var diseasename = $('#inputTag').val().trim();
        if(!doctorid || !doctorusername || doctorusername != $('#inputAuthor').attr('data-name')){
            $$.modal.tips('请输入模板作者',1500);
            return false;
        }
        if(!name){
            $$.modal.tips('请输入模板名称',1500);
            return false;
        }
        new followupPlan().setFollowupTempBase(urlParamObj.templateid,doctorid,doctorusername,name,diseasename,function (res,status) {
            res = JSON.parse(res);
            if(res.code = '000'){
                var data = JSON.parse(res.data);
                $$.modal.tips('保存成功',3000);
                setTimeout(function () {
                    location.href = './edit-node.html?templateid=' + data.id;
                },1000);
            }else{
                $$.modal.tips('网络错误',3000);
            }
        });
    });
};
//是否编辑信息========
var editorNode=function(){
    new followupPlan().getFollowupTempInfo(urlParamObj.templateid,function(res,status){
        res = JSON.parse(res);
        var resData = JSON.parse(res.data);
        if(res.code == '000'){
            $('#inputAuthor').attr({'data-id':resData.doctorid,'data-name':resData.doctorusername});
            $('#inputAuthor').val(resData.doctorusername);
            $('#inputName').val(resData.name);
            $('#inputTag').val(resData.diseasename);
        }
    })
};

$(function () {
    calcFormHeight();
    $(window).resize(function () {
        calcFormHeight();
    });
    matchAuthorInfo();
    setTemplateBase();
    editorNode();
    $('.outer').addClass('active');
});